package com.link.base.base.redpacketconfig.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.link.base.base.redpacketconfig.model.RedPktCfg;
import com.link.core.cllog.LCLogger;
import com.link.base.base.redpacketconfig.service.RedPktGlobalCfgService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.link.base.base.redpacketconfig.model.RedPktGlobalCfg;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.KeyGenerateService;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: 闫有霞
 * @date: 2019-01-23
 * @comments: 红包全局设置
 */
@Controller
@RequestMapping("/link/redPktGlobalCfg")
public class RedPktGlobalCfgController extends BasicController<RedPktGlobalCfg> {

    @Resource
    private KeyGenerateService keyGenerateService;

    @Resource
    private RedPktGlobalCfgService redPktGlobalCfgService;

    @Override
    public BasicService<RedPktGlobalCfg> getBasicService() throws Exception {
        return redPktGlobalCfgService;
    }

    /**
     * @createdBy 闫有霞
     * @date 2019/1/25
     * @methods: upsertRedCfg
     * @param: RedPktCfg entity
     * @return:  result
     * @description: 当数据库有数据时更新记录，没有数据时添加记录
     **/
    @RequestMapping(value = "/upsertRedCfg")
    @ResponseBody
    public Map<String, Object> upsertRedCfg(@RequestBody RedPktCfg entity, HttpSession session,
                                            HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<>(16);
        try {
            redPktGlobalCfgService.upsertRedCfg(entity);
            result.put("success", true);
            result.put("result", entity);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * @createdBy 闫有霞
     * @date 2019/1/25
     * @methods: queryRedCfg
     * @param: RedPktCfg entity
     * @return:  result
     * @description: 查询红包设置数据
     **/
    @RequestMapping(value = "/queryRedCfg")
    @ResponseBody
    public Map<String, Object> queryRedCfg(@RequestBody RedPktCfg entity, HttpSession session,
                                           HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<>(16);
        try {
            RedPktCfg rows = redPktGlobalCfgService.queryRedCfg(entity);
            result.put("success", true);
            result.put("result", rows);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }
}
